﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Churchtoolkit.Utils.ErrorProvider;
using Churchtoolkit.Web.Shell.Views;
using Churchtoolkit.Web.Shell.Presenters;
using Churchtoolkit.DataAccess.Entities;

namespace Churchtoolkit.Web
{
    public partial class PersonalInfoView : Page<PersonalInfoPresenter>, IPersonalInfoView
    {
        #region [Properties]
        private Members _MemberPersonalInfo
        {
            get { return this.Presenter.GetCurrentMemberPersonalInfo(); }
        }
        private List<MemberPhones> _MemberPhones
        {
            get { return this.Presenter.GetMemberPhones(); }
        }
        private MemberAddresses _MemberAddresses
        {
            get { return this.Presenter.GetMemberAddresses(); }
        }
        #endregion
        #region[Events]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillNationalityddl();
                FillCountryddl();
                FillMemberPersonalInfoFile();

                cNotification.Show("success");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveMemberPersonalInfo();
        }

        #endregion
        #region [HelperFunction]
        private void FillNationalityddl()
        {
            ddlNationality.DataSource = this.Presenter.GetNationalities();
            ddlNationality.DataTextField = "NationalityName";
            ddlNationality.DataValueField = "NationalityID";
            ddlNationality.DataBind();
        }
        private void FillCountryddl()
        {
            ddlCountry.DataSource = this.Presenter.GetCountries();
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryID";
            ddlCountry.DataBind();
        }
        private void FillMemberPersonalInfoFile()
        {
            txtFirstName.Text = _MemberPersonalInfo.FirstName;
            txtLastName.Text = _MemberPersonalInfo.LastName;
            txtDisplayName.Text = _MemberPersonalInfo.DisplayName;
            if (_MemberPersonalInfo.Gender == 0)
                rdbMale.Checked = true;
            else rdbFemale.Checked = true;
            uxBirthDate.SelectedDate = (DateTime)_MemberPersonalInfo.Birthdate;
            ddlCountry.SelectedValue = _MemberPersonalInfo.CountryId.ToString();
            ddlNationality.SelectedValue = _MemberPersonalInfo.NationalityId.ToString();

            foreach (MemberPhones Phone in _MemberPhones)
            {
                if (Phone.PhoneType == 1)
                {
                    txtPhone.Text = Phone.Phone;
                }
                else if (Phone.PhoneType == 2)
                {
                    txtExt.Text = Phone.Phone.Substring(0, Phone.Phone.IndexOf("-"));
                    txtMobile.Text = Phone.Phone.Substring(Phone.Phone.IndexOf("-") + 1, Phone.Phone.Length - Phone.Phone.IndexOf("-") - 1);
                }
            }

            txtStreet.Text = _MemberAddresses.Street;
            txtBuilding.Text = _MemberAddresses.BuildingNumber;
            txtFloor.Text = _MemberAddresses.FloorNumber;
            txtAppartment.Text = _MemberAddresses.AppartmentNumber;
            txtZipCode.Text = _MemberAddresses.ZipCode;
            txtDirections.Text = _MemberAddresses.Directions;

        }
        private void SaveMemberPersonalInfo()
        {
            if (IsValid)
            {
                Members MemberPersonalInfo = _MemberPersonalInfo;
                MemberPersonalInfo.FirstName = txtFirstName.Text;
                MemberPersonalInfo.LastName = txtLastName.Text;
                MemberPersonalInfo.DisplayName = txtDisplayName.Text;
                if (rdbMale.Checked == true)
                    MemberPersonalInfo.Gender = 0;
                else MemberPersonalInfo.Gender = 1;
                MemberPersonalInfo.Birthdate = uxBirthDate.SelectedDate;
                MemberPersonalInfo.CountryId = Convert.ToInt32(ddlCountry.SelectedValue);
                MemberPersonalInfo.NationalityId = Convert.ToInt32(ddlNationality.SelectedValue);
                this.Presenter.UpdateMemberInfo(MemberPersonalInfo);

                List<MemberPhones> MemberPhoness = _MemberPhones;
                if (MemberPhoness.Count != 0)
                {
                    foreach (MemberPhones phone in MemberPhoness)
                    {
                        if (phone.PhoneType == 0)
                            phone.Phone = txtPhone.Text;
                        else if (phone.PhoneType == 2)
                            phone.Phone = txtExt.Text + "-" + txtMobile.Text;
                    }
                    this.Presenter.UpdateMemberPhones(MemberPhoness.ToArray());
                }
                else if (MemberPhoness.Count == 0)
                {
                    MemberPhones item = new MemberPhones();
                    item.Phone = txtPhone.Text;
                    item.PhoneType = 1;
                    MemberPhoness.Add(item);
                    item.Phone = txtExt.Text + "-" + txtMobile.Text;
                    item.PhoneType = 2;
                    MemberPhoness.Add(item);
                    this.Presenter.AddMemberPhones(MemberPhoness.ToArray());
                }
                else
                {
                    if (MemberPhoness[0].PhoneType == 1)
                    {
                        this.Presenter.UpdateMemberPhones(MemberPhoness.ToArray());
                        if (txtExt.Text + txtMobile.Text != null)
                        {
                            MemberPhones item = new MemberPhones();
                            item.Phone = txtExt.Text + "-" + txtMobile.Text;
                            item.PhoneType = 2;
                            MemberPhoness.Add(item);
                            this.Presenter.AddMemberPhones(MemberPhoness.ToArray());
                        }
                    }
                    else if (MemberPhoness[0].PhoneType == 2)
                    {
                        this.Presenter.UpdateMemberPhones(MemberPhoness.ToArray());
                        if (txtExt.Text + txtMobile.Text != null)
                        {
                            MemberPhones item = new MemberPhones();
                            item.Phone = txtExt.Text + "-" + txtMobile.Text;
                            item.PhoneType = 2;
                            MemberPhoness.Add(item);
                            this.Presenter.AddMemberPhones(MemberPhoness.ToArray());
                        }
                    }
                }

                MemberAddresses MemberAddresses = _MemberAddresses;
                MemberAddresses.Street = txtStreet.Text;
                MemberAddresses.BuildingNumber = txtBuilding.Text;
                MemberAddresses.FloorNumber = txtFloor.Text;
                MemberAddresses.AppartmentNumber = txtAppartment.Text;
                MemberAddresses.ZipCode = txtZipCode.Text;
                MemberAddresses.Directions = txtDirections.Text;

                if (MemberAddresses.AddressId != 0)
                    this.Presenter.UpdateMemberAddresses(MemberAddresses);
                else
                    this.Presenter.AddMemberAddresses(MemberAddresses);
            }
        }
        #endregion
        #region [ServerValidateFunction]
        protected void cvldtxtZipcode_ServerValidate(object source, ServerValidateEventArgs args)
        {
            for (int i = 0; i <= args.Value.Length - 1; i++)
            {
                if (!Char.IsDigit(args.Value, i))
                {
                    args.IsValid = false;
                    return;
                }
            }
            args.IsValid = true;
        }
        protected void cvldtxtLastName_ServerValidate(object source, ServerValidateEventArgs args)
        {
            for (int i = 0; i <= args.Value.Length - 1; i++)
            {
                if (!Char.IsLetter(args.Value, i))
                {
                    args.IsValid = false;
                    return;
                }
            }
            args.IsValid = true;
        }
        protected void cvlduxBirthDate_ServerValidate(object source, ServerValidateEventArgs args)
        {
            DateTime SelectedDate = Convert.ToDateTime(args.Value);
            if (SelectedDate.Year >= DateTime.Now.Year - 18)
            {
                args.IsValid = false;
                return;
            }
            args.IsValid = true;
        }
        #endregion
    }
}